package com.asm.repository;

import com.asm.entity.sys.WorkOrder;
import com.asm.vo.sys.WorkOrderVo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author zhangyufu
 * @description 定义工单操作的接口
 * @date 2019/7/12
 */
@Repository
public interface WorkOrderRepository extends JpaRepository<WorkOrder, Long> {

    /**
     * 根据id查询工单
     *
     * @param id 工单编号
     * @return 工单
     */
    @Query("select w from WorkOrder w where w.isDel=false and w.id=:id")
    WorkOrder findWorkOrderById(Long id);

    /**
     * 查询指定状态的工单信息
     *
     * @param status 状态
     * @param isDel 是否已经被删除
     * @param page 分页
     * @return 工单列表
     */
    Page<WorkOrder> findByStatusAndIsDel(String status, Boolean isDel, Pageable page);

    /**
     * 查找所有工单
     *
     * @param of 分页
     * @return 工单列表
     */
    @Query("select c from WorkOrder c where c.isDel=false ")
    Page<WorkOrder> findAll(PageRequest of);

    /**
     * 根据工单类型查找工单
     *
     * @param workType 工单类型
     * @return 工单列表
     */
    @Query("select w from WorkOrder w where w.workType=:workType and w.isDel=false ")
    List<WorkOrder> findWorkOrderByWorkType(String workType);

    /**
     * 根据工人编号查找工单
     *
     * @param workerId 工人编号
     * @param page 分页
     * @return 工单列表
     */
    @Query("select c from WorkOrder c where c.workerId=:workerId and c.isDel=false ")
    Page<WorkOrder> findByWorkerId(Long workerId, Pageable page);

    /**
     * 根据业务员编号查找工单
     *
     * @param sellId 业务员编号
     * @param page 分页
     * @return 工单列表
     */
    @Query("select new Map(w.id as taskId, m.sellId as sellId, w.workerId as workerId, m.spotId as spotId, w.workType as taskContent, w.status as taskStatus) from WorkOrder w inner " +
            "join Maintenance m on w.id=m.workerOrderId where m.sellId=?1")
    Page<Map<String, Object>> findBySellId(Long sellId, Pageable page);

}
